<?php include "../../layout/header.php"; ?>
<script type="text/javascript" src="<?php echo($siteroot);?>js/shop/search.js"></script>
<script type="text/javascript" src="<?php echo($siteroot);?>js/shop/cartControl.js"></script>
<link rel="stylesheet" href="<?php echo($siteroot);?>css/shop.css" type="text/css" />

<?php
$purchase = 0;
// checking out function
if(isset($_POST['total'])){
	if(isset($_SESSION['items']) || count($_SESSION['items']) > 0){
		include "../../db/dbconnect.php";
		//if(isset($_SESSION['userinfo'])) // if user is signed in
		if(isset($unserializedUser))
        {
        	//require_once $localroot."includes/basicInfo/user.php";
            //$serializedUser = $_SESSION['userinfo'];
            $unserializedUser = unserialize($serializedUser);
            $gold = $unserializedUser->getGold();
            $user_id = $unserializedUser->getUid();
        }
		$total = $_POST['total'];
		foreach($_SESSION['items'] as $item_id => $amount){
			$query = "SELECT inventory_id,user_id,item_id,amount FROM inventory WHERE user_id={$user_id} AND item_id={$item_id}";
			$result = mysql_query($query);
			if(mysql_num_rows($result) > 0){ // if user has the item
				$row = mysql_fetch_array($result);
				$inventory_id = $row['inventory_id'];
				$num = $row['amount'] + $amount;
				$add_query = "UPDATE inventory SET amount={$num} WHERE inventory_id={$inventory_id}";
			}else{
				$add_query = "INSERT INTO inventory (user_id,slot_num,item_id,amount,equipped)" .
						"VALUES ({$user_id},0,{$item_id},{$amount},0)";
			}
			$update = mysql_query($add_query) or die(mysql_error());
			//if($update){echo "true";}else{echo "false";} // test
		}
		
		if(isset($unserializedUser)){
			$gold = $unserializedUser->getGold() - $total;
			$unserializedUser->setGold($gold);
			$serialzedUser = serialize($unserializedUser);
        	$_SESSION['userinfo']=$serialzedUser;
		}else{
			$query = "SELECT gold FROM User WHERE uid={$user_id}";
			$result = mysql_query($query);
			$row = mysql_fetch_array($result);
			$gold = $row['gold'];
		}
		$sub_query = "UPDATE User SET gold={$gold} WHERE uid={$user_id}";
		mysql_query($sub_query);
		unset($_SESSION['items']);
		$purchase = 1;
	
	}
}
?>

<?php include "../../layout/navleft.php"; ?>

<div id="content_medium_left"><div class="container_brown">
	<font face="Verdana" color = brown size = 2.5px> <a href="mainShop.php" >Store</a></font>
    <font face="Verdana" color = brown size = 2.5px> > <a href="ItemShop.php" >Item Shop</a>
    </font> <font face="Verdana" color =brown size = 2.5px> > Shopping Cart</font><br>


<center>
	<table id="shopping_cart_table">
		<thead>
        	<tr>
	            <th width=10%>&nbsp&nbsp&nbsp&nbsp</th>
	            <th width=20%>NAME</th>
	            <th width=30%>DESCRIPTION</th>
	            <th width=5%>QTY</th>
	            <th width=10%>UNIT PRICE</th>
	            <th width=10%>SUBTOTAL</th>
	            <th width=5%></th>
			</tr>
		</thead>

<?php
if(isset($unserializedUser)) // if user is signed in
{
    //$serializedUser = $_SESSION['userinfo'];
    //$unserializedUser = unserialize($serializedUser);
    $gold = $unserializedUser->getGold();
}
else{ $gold = 0; } // test
?>

<?php
$disable = "";
$checkOutAmount =0;

if(!isset($_SESSION['items']) || count($_SESSION['items']) == 0){
	if($purchase == 1)
		echo "<p>Purchase completed: Thank you!</p>";
	if(!isset($_SESSION['logged_in'])){?>
		<p class="main_shop_head">Please sign in.</p>
	<?php }else{
    	echo "<p>Shopping cart is empty.</p>";
	}
	$disable = "disabled ";
}else{
	$i = 0;
	$item_total = count($_SESSION['items']);
	$item_temp = "";
	foreach($_SESSION['items'] as $key => $item){
		$item_temp .= $key;
		if($i < $item_total - 1){
			$item_temp .= ',';
		}
		$i++;
	}
	include "../../db/dbconnect.php";
	$query = 'SELECT item_id,name,price_buy,file,effects FROM item WHERE item_id in ('.$item_temp.')';
	$result = mysql_query($query);
	$cnt = mysql_num_rows($result);
	
    $count = 0;
    while($row = mysql_fetch_array($result))
    {
        $count = $count+1;
        $id = $row['item_id'];

        $img = "../../images/shop/".$row['file'];
        $description = $row['effects'];
        
        $name = $row['name'];
        $price = $row['price_buy'];
        $qty = $_SESSION['items'][$id];
        $total = $price * $qty;
        $checkOutAmount = $checkOutAmount +$total;

        echo "<tr>
        <td><img src=\"{$img}\"></td>
        <td>{$name}</td>
        <td>{$description}</td>
        <td><input type=text class=\"quantity\" name=\"{$id}\" value=\"{$qty}\" size =\"2\" onchange=\"changeQuantity(this,{$gold})\"/></td>
        <td>{$price}</td>
        <td>{$total}</td>
        <td> <input type=\"image\" src=\"../../images/shop/ico_remove.gif\" onclick=\"updateCart({$id})\"/></td>
        </tr>";
    }
}
if(($gold - $checkOutAmount) < 0) $disable = "disabled ";
?>    
	</table>
</center>


<form action="shoppingCart.php" method="POST">
	<br>
	<div id="sc_total_div">
	<table id="sc_total_table">
		<tr>
			<td>Total:</td>
			<td width="70"><div id="total_price"><?php echo $checkOutAmount;?></div>
			<input id="total_price_hidden" type="hidden" name="total" value="<?php echo $checkOutAmount;?>"/></td>
		</tr>
		<tr>
			<td>Your gold:</td>
			<td><?php echo($gold);?></div>
		</tr>
		<tr>
			<td><div class="gold_change" <?php if(($gold - $checkOutAmount) < 0){ echo('style="color:#ff0000;"'); $disable='disabled ';}?>>Change:</div></td>
			<td><div class="gold_change" <?php if(($gold - $checkOutAmount) < 0) echo('style="color:#ff0000;"');?>>
				<div id="total_price_change"><?php echo($gold - $checkOutAmount);?></div></div></td>
		</tr>
		<tr>
			<?php if($disable == ""){?>
			<td colspan="2"><input type="image" id="checkout_button" name="checkout" src="../../images/shop/bt_checkout.gif" ></td>
			<?php }?>
		</tr>
	</table>
	</div>
</form>

</div></div>
<?php include "../../layout/footer.php"; ?>